package chabernac.android.wordbattle.paint.classic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import android.graphics.Color;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.model.WordRaster;
import chabernac.android.wordbattle.paint.ClearAllDrawable;
import chabernac.android.wordbattle.paint.DrawableComposite;
import chabernac.android.wordbattle.paint.iDrawable;
import chabernac.android.wordbattle.paint.iDrawableFactory;

public class HighlightDrawableFactory implements iDrawableFactory{
  private static Set<Integer> USED_COLORS = new HashSet<Integer>();
  private static Random RANDOM = new Random();

  private final Map<Word, iDrawable> myWordDrawables = new HashMap<Word, iDrawable>();
  
  private int myCurrentSelectionColor = getRandomColor();
  
  private iDrawable myWordRasterDrawable = null;
  private iDrawable myClearAllDraweble  = null;
  
  private List<Letter> mySelectedLetters = new ArrayList<Letter>();
  
  public HighlightDrawableFactory() {
    super();
  }
  
  private iDrawable createDrawable(Word aWord){
    if(aWord.isLettersOnLine()){
      return new WordPictureDrawable(aWord, myCurrentSelectionColor);
    } else {
      return new WordBitmapDrawable(aWord, myCurrentSelectionColor);
    }
  }

  public iDrawable createWordDrawable(Word aWord){
    if(!myWordDrawables.containsKey(aWord)){
      myWordDrawables.put(aWord, createDrawable(aWord));
      myCurrentSelectionColor = getRandomColor();
    }
    return myWordDrawables.get(aWord);
  }
  
  public iDrawable createSelectedLettersDrawable(Word aWord){
    return createDrawable(aWord);
  }

  private static int randomColor(){
    return 50 + Math.abs(RANDOM.nextInt() % 5) * 40;
  }

  protected static int getRandomColor( ) {
    int theRandomColor = Color.argb( 0, randomColor(), randomColor(), randomColor()); 
    while(USED_COLORS.contains(theRandomColor)){
      theRandomColor = Color.argb( 0, randomColor(), randomColor(), randomColor());
    }
    USED_COLORS.add(theRandomColor);
    return theRandomColor;
  }

  private iDrawable createSelectedWordsDrawable(WordRaster aRaster) {
    DrawableComposite theCompositeDrawable = new DrawableComposite();
    for(Word theWord : aRaster.getFoundWordsByLocalPlayer()){
      theCompositeDrawable.addDrawable( createWordDrawable( theWord )  );
    }
    return theCompositeDrawable;
  }

  @Override
  public iDrawable createGameDrawable(WordRaster aRaster) {
    DrawableComposite theComposite = new DrawableComposite();
    
    theComposite.addDrawable( createClearAllDrawable() );
    
    theComposite.addDrawable( createWordRasterDrawable( aRaster ));
    
    theComposite.addDrawable( createSelectedWordsDrawable(aRaster) );
    
    List<Letter> theSelectedLetters = aRaster.getSelectedLetters();
    if(!theSelectedLetters.isEmpty()){
      theComposite.addDrawable( createSelectedLettersDrawable(new Word(theSelectedLetters)));
    }
//    mySelectedLetters = new ArrayList<Letter>(aRaster.getSelectedLetters());
    
    return theComposite;
  }

  private iDrawable createWordRasterDrawable( WordRaster aRaster ) {
    if(myWordRasterDrawable == null) {
      myWordRasterDrawable = new WordRasterDrawable( aRaster );
    }
    return myWordRasterDrawable;
  }

  private iDrawable createClearAllDrawable() {
    if(myClearAllDraweble == null){
      myClearAllDraweble = new ClearAllDrawable( Color.LTGRAY );
    }
    return myClearAllDraweble;
  }
}
